// index.js
// 获取应用实例
import {getBanner, getRank, getRecommend} from '../../service/index'

Page({
  data: {
    bannerList:[], //轮播图
    recommendList:[], //推荐歌单
    rankList:[] //排行榜
  },

  // 请求轮播图
  async getbannerListData(){
    let res = await getBanner({type:2});
    this.setData({
      bannerList:res.banners
    })
  },
  // 请求推荐歌单
  async getRecommendListData(){
    let res = await getRecommend({limit:10});
    this.setData({
      recommendList:res.result
    })
  },
  // 请求排行榜
  async getRankListData(){
    // let idx = 0;
    // let resultArr = [];
    // // 发送五次请求
    // while(idx <5){
    //   let res = await getRank({idx:idx++});
    //   // 过滤数据,只取playlist.name和.playlist.tracks数组的前三条数据
    //   let filterItem = {name:res.playlist.name,tracks:res.playlist.tracks.slice(0,3)};
    //   resultArr.push(filterItem);
    // }
      let res = await getRank();
    this.setData({
      rankList:res.list.slice(0,5)
    })
  },
  // 处理nav栏点击跳转
  toPageHandle(){
    wx.navigateTo({
      url: '/pages/subpackages/pages/recommendationSong/recommendationSong',
    })
  },
    // 跳转到搜索音乐页面
    toSearch(){
      wx.switchTab({
        url: '/pages/search/search',
      })
    },
    toRecommend(){
      wx.navigateTo({
        url: '/pages/subpackages/pages/recommendationSong/recommendationSong',
      })
    },

  onLoad() {
    this.getbannerListData();
    this.getRecommendListData();
    this.getRankListData();
  },

})
